//
//  CollectionViewController.swift
//  HMWeibo007
//
//  Created by TTang on 15/7/3.
//  Copyright © 2015年 shanyu. All rights reserved.
//

import UIKit
import FFAutoLayout
private let reuseIdentifier = "Cell"

class  NewFeatureController: UICollectionViewController {
    //定义布局
    let layout = UICollectionViewFlowLayout()
    init(){
    
        super.init(collectionViewLayout: layout)
    }
    

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册可重用cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print(self.collectionView?.frame)
        //设置布局
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    
    
    
    // MARK: UICollectionViewDataSource

    //定义图片数量
    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
    
        cell.imageIndex = indexPath.item
    
        return cell
    }
    ///  CollectionView 停止滚动的方法
    ///
    ///  :param: collectionView  CollectionView
    ///  :param: cell           之前显示的cell
    ///  :param: indexPath      之前显示的indexpath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前显示的cell
        let path = collectionView.indexPathsForVisibleItems().last!
        //只在最后一页显示按钮
        if path.item == imageCount - 1 {
           //拿到cell后，开始动画
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).startAnimation()
        }
    }

}

class  NewFeatureCell: UICollectionViewCell {
    /// 图像索引
    var imageIndex : Int = 0 {
        didSet{
            iconView.image = UIImage(named:"new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    
    ///‘开始体验’ 按钮的动画方法
    func startAnimation()
    {
         startButton.transform = CGAffineTransformMakeScale(0, 0)
         startButton.hidden = false
         startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
               self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("OK")
                self.startButton.userInteractionEnabled = true
        }
        
    }
    
    func startButtonClicked(){
        // object = true 表示显示 MainViewController
        NSNotificationCenter.defaultCenter().postNotificationName(HMSwitchRootVCNotification, object: true)
    }
    
    
    lazy var iconView : UIImageView = {
        
        return UIImageView()
    }()
    
    
    lazy var startButton: UIButton = {
        
        let btn = UIButton()
        btn.frame = CGRectMake(0, 0, 105, 36)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
      
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "startButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconView)
        addSubview(startButton)
        
        //设置布局
        startButton.ff_AlignInner(ff_AlignType.BottomCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -160))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        iconView.frame = self.bounds
    }
    
    
    
}
